package com.zyzd.baseokhttp.base.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zyzd.baseokhttp.R;
import com.zyzd.baseokhttp.interf.IBackConstant;
import com.zyzd.baseokhttp.interf.OnCompleteListener;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by 李宗源 on 2017/3/9.
 * <p>
 */

public class BackActivtiy extends BaseActivity implements Toolbar.OnMenuItemClickListener, IBackConstant {

    @Bind(R.id.toolbar_title)
    TextView mToolbarTitle;
    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    @Bind(R.id.iv_bg)
    ImageView mIvBg;
    @Bind(R.id.tv_left)
    TextView mTvLeft;
    /**
     * 当前Fragment类型
     */
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        type = intent.getStringExtra(EXTRA_TYPE);
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_back;
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
//        type = intent.getStringExtra(EXTRA_INDEX);
        setToolBar(intent.getStringExtra(EXTRA_TITLE));
        loadBgImage();
        repleceFragment(intent);
    }

    /**
     * 替换fragment
     *
     * @param intent
     */
    private void repleceFragment(Intent intent) {

        Fragment fragment = null;
        switch (type) {
//            case TYPE_BIND_ALIPAY:
//                fragment = new BindAliPayFragment();
//                break;
        }
        fragment.setArguments(intent.getBundleExtra(EXTRA_DATA));
        replaceFragment(fragment);
    }

    public void replaceFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment, type).commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        switch (type) {
            case TYPE_PERSON_DATA_EDIT:
            case TYPE_CREATE_ORDER:
            case TYPE_APPLY_ENTER_FOR_BEAUTY:
            case TYPE_CONTENT_EDIT:
            case TYPE_APPLY_CASH:
            case TYPE_ALIPAY_INFO:
                getMenuInflater().inflate(R.menu.menu_complete, menu);
//                if (type.equals(TYPE_CREATE_ORDER)) {
//                    menu.findItem(R.id.action_complete).setTitle(getString(R.string.save));
//                } else if (type.equals(TYPE_APPLY_CASH)) {
//                    menu.findItem(R.id.action_complete).setTitle(getString(R.string.cash_record));
//                } else if (type.equals(TYPE_ALIPAY_INFO)) {
//                    menu.findItem(R.id.action_complete).setTitle(getString(R.string.modify));
//                }
                return true;
            default:
                return super.onCreateOptionsMenu(menu);
        }
    }

    /**
     * 设置标题
     */
    public void setTitle(String title) {
        mToolbarTitle.setText(title);
    }

    protected void setToolBar(String title) {
        mToolbar.setTitle("");
        setTitle(title);
        setSupportActionBar(mToolbar);

        switch (type) {
//            case TYPE_CONTENT_EDIT:
//                mTvLeft.setText(getString(R.string.cancel));
//                mTvLeft.setVisibility(View.VISIBLE);
//                break;
            default:
                mToolbar.setNavigationIcon(R.mipmap.ic_back);
                break;
        }
    }

    private void loadBgImage() {
        switch (type) {
            case TYPE_HOSPITAL_INFO:
//                String imageUrl = getIntent().getBundleExtra(EXTRA_DATA).getString(EXTRA_IMAGE_URL);
//                Glide.with(this).load(imageUrl).into(mIvBg);
                break;
            default:
//                ImageUtil.loadMainBg(this, mIvBg);
                break;

        }

    }

    @Override
    public void setListener() {
        mToolbar.setOnMenuItemClickListener(this);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_complete:
                Fragment fragment = getSupportFragmentManager().findFragmentByTag(type);
                if (fragment instanceof OnCompleteListener) {
                    ((OnCompleteListener) fragment).onConfirm();
                }
                break;
        }
        return true;
    }

    @OnClick(R.id.tv_left)
    public void onClick() {
        finish();
    }

    /**
     * 实现沉浸式通知栏与导航栏
     *
     * @param hasFocus 是否有焦点
     */
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
//                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            );
        }
    }

    @Override
    protected void setSelfTheme() {
//        switch (type) {
//            case TYPE_APPLY_CASH:
//                setTheme(R.style.apply_cash_theme);
//                break;
//        }
    }
}
